<template>
	<view class="container">
		<view class="container-top">
			<view class="head-top" v-if="user!=''">
				<view class="head-top-left">
					<image :src="user.head_pic" />
					<text>{{user.nickname}}</text>
				</view>
				<view class="wrapper">
					<view class="head-top-right">
						<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/jifen/jifen-icon.png"
							mode="widthFix" />
						<text>{{user.jifen}}积分</text>
					</view>
					<view class="center-item" :class="{'act':currentIndex==2}" @click="toRecord">
						<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/jifen/jifen-buy-order.png"
							mode="heightFix" />
						<view class="tit">兑换记录</view>
					</view>
				</view>
			</view>
			<button class="head-top" v-else>
				<view class="head-top-left">
					<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/test.jpg" />
					<text class="text">未登录</text>
				</view>
				<view class="wrapper">
					<view class="head-top-right">
						<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/jifen/jifen-icon.png"
							mode="widthFix" />
						<text>0.00积分</text>
					</view>
					<view class="center-item" :class="{'act':currentIndex==2}" @click="toRecord">
						<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/jifen/jifen-buy-order.png"
							mode="heightFix" />
						<view class="tit">兑换记录</view>
					</view>
				</view>
			</button>
		</view>
		<!-- <view class="center">
			<view class="center-item" :class="{'act':currentIndex==1}" @click="changeCurrent(1)">
				<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/jifen/jifen-buy.png" mode="heightFix" />
				<view class="tit">积分购物</view>
			</view>
			<view class="center-item" :class="{'act':currentIndex==2}" @click="changeCurrent(2)">
				<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/jifen/jifen-buy-order.png" mode="heightFix" />
				<view class="tit">购物订单</view>
			</view>
		</view> -->
		<view class="goods-container">
			<block v-if="currentIndex==1">
				<my-waterfalls-goods :list="goods" :autoJump="false" @wapperClick="wapperClick"></my-waterfalls-goods>
				<u-loadmore :status="hasMore?'loading':'nomore'" />
			</block>
			<view class="order-container" v-if="currentIndex==2">
				<scroll-view scroll-y class="scroll" @scrolltolower="loadMoreOrder">
					<view class="order-items" v-if="order_list.length>0">
						<view class="order-item" v-for="(item,index) in order_list" :key="index"
							@click="toJfDetail(item,index)">
							<view class="top">
								<view class="left">{{item.time_text}}</view>
								<view class="right">{{item.status_text}}</view>
							</view>
							<view class="order-goods-rows">
								<view class="order-goods-row-item">
									<image :src="item.goods_img" mode="widthFix" class="goods-img"></image>
									<view class="right">
										<view class="goods-name">{{item.goods_name}}</view>
										<view class="right-bot">
											<view class="price">
												<text>￥</text>
												<text class="large">{{item.goods_price}}</text>
											</view>
											<view class="num">x{{item.goods_num}}</view>
										</view>
									</view>
								</view>
							</view>
							<view class="total">
								<text class="black">实付:</text>
								<text>￥</text>
								<text class="large">{{item.order_amount}}</text>
							</view>
							<view class="order-bot">
								<button size="mini">查看订单</button>
								<button size="mini" class="confirm" v-if="item.order_status==2"
									@click.stop="jf_order_confirm_show(item)">确认收货</button>
							</view>
						</view>
					</view>
					<view class="empty-container" v-else>
						<u-empty text="暂无相关订单"
							icon="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/common/empty.png" />
					</view>
				</scroll-view>
			</view>
		</view>
		<u-popup :safeAreaInsetBottom="false" :safeAreaInsetTop="false" mode="center" :show="sh_show" round="5"
			:overlay="true" :customStyle="customStyle1" @close="sh_show=false">
			<view class="u-popup-slot">
				<text class="u-popup-slot_text">确认收货？</text>
				<view class="u-popup-slot_flex">
					<button @click="sh_show = !sh_show" class="cancel">取消</button>
					<button @click="jf_order_confirm()" class="confirm">确认</button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	var _this;
	export default {
		data() {
			return {
				customStyle1: {
					'width': '80%'
				},
				currentIndex: 1,
				user: '',
				nickname: '',
				user_points: '0.00',
				goods: [],
				hasMore: false,
				pageIndex: 1,
				yet_order_load: false,
				order_page: 1,
				order_hasMore: true,
				order_list: [],
				sh_show: false,
				temp_order: {}
			};
		},
		methods: {
			jf_order_confirm_show(item) {
				_this.temp_order = item
				_this.sh_show = true
			},
			async jf_order_confirm() {
				const params = {
					id: _this.temp_order.order_id,
				}
				//发送请求
				const data = await _this.$post('api/jf_dd_confirm', params)
				if (data.code == 200) {
					uni.$u.toast(data.msg)
					_this.temp_order['order_status'] = 3
					_this.temp_order['status_text'] = '已完成'
					_this.sh_show = false
				} else {
					uni.$u.toast(data.msg)
				}
			},
			changeCurrent(current) {
				if (_this.currentIndex == current) {
					return
				}
				_this.currentIndex = current
				if (current == 2 && !_this.yet_order_load) {
					_this.get_jf_dd_list()
				}
			},
			toRecord() {
				uni.navigateTo({
					url: '/pages/jf-shop/record'
				})
			},
			//获取用户信息
			async get_user_info() {
				const data = await _this.$post('api/UserInfo')
				if (data.code == 200) {
					this.user = data.data.user
				} else {
					this.$u.toast(data.msg)
				}
			},
			//获取积分商品列表
			async get_jf_goods() {
				const params = {
					pageIndex: _this.pageIndex,
					pageSize: 20,
				}
				const data = await _this.$post('api/get_jf_goods')
				if (data.code == 200) {
					_this.goods.push(...data.data.list)
					_this.hasMore = data.data.list.length >= params.pageSize
				} else {
					_this.hasMore = false
				}
			},
			goList() {
				this.$u.route('pages/center/order')
			},
			wapperClick(item) {
				this.$u.route({
					url: '/pages/jf-shop/jf-goods-detail',
					params: {
						id: item.goods_id
					}
				})
			},
			async get_jf_dd_list() {
				const params = {
					pageIndex: _this.order_page,
					pageSize: 20,
				}
				const data = await _this.$post('api/get_jf_dd_list', params)
				if (data.code == 200) {
					_this.yet_order_load = true
					this.order_list.push(...data.data.list)
					this.order_hasMore = data.data.list.length >= params.pageSize
				} else {
					this.order_hasMore = false
				}
			},
			toJfDetail(item, index) {
				uni.navigateTo({
					url: `/pages/order/jf-order-detail?id=${item.order_id}&index=${index}`
				})
			},
		},
		onReachBottom() {
			if (_this.currentIndex == 1) {
				if (_this.hasMore) {
					_this.pageIndex++
					_this.get_jf_goods()
				}
			} else {
				if (_this.order_hasMore) {
					_this.order_page++
					_this.get_jf_dd_list()
				}
			}
		},
		onLoad(e) {
			_this = this;
			uni.$on('jfOrderConfirm', index => {
				_this.order_list[index]['order_status'] = 3
				_this.order_list[index]['status_text'] = '已完成'
			})
			_this.get_jf_goods()
		},
		onUnload() {
			uni.$off('jfOrderConfirm')
		},
		onPullDownRefresh() {
			_this.pageIndex = 1
			_this.goods = []
			_this.get_jf_goods()
			uni.stopPullDownRefresh()
		},
		onShow() {
			_this.get_user_info()
		},
		components: {}
	}
</script>

<style lang="scss" scoped>
	@import "@/static/css/order.scss";

	.container {
		position: relative;
		padding: 0;

		.container-top {
			padding: 20rpx 30rpx 0 30rpx;
			height: 160rpx;
			background: $theme-color;

			.head-top {
				display: flex;
				align-items: center;
				justify-content: space-between;
				flex: 1;
				background: none;
				padding: 0;
				.wrapper {
					display: flex;
					flex-direction: column;
					.center-item {
						margin-top: 10rpx;
						position: relative;
						@include flex-box(row, center, center);
						background: #fff;
						height: 56rpx;
						border-radius: 28rpx;
						padding: 0 15rpx;
						image {
							height: 38rpx;
							margin-right: 10rpx;
						}
					
						.tit {
							color: $theme-color;
							font-size: 24rpx;
						}
					}
					>view {
						display: flex;
						align-items: center;
					}
				}
				>view {
					display: flex;
					align-items: center;
				}

				.head-top-left {
					image {
						width: 110rpx;
						height: 110rpx;
						border-radius: 50%;
						margin-right: 20rpx;
					}

					text {
						color: #fff;
						font-size: 30rpx;
					}

					.text {
						font-size: 30rpx;
					}
				}

				.head-top-right {
					background: #fff;
					height: 56rpx;
					border-radius: 28rpx;
					padding: 0 15rpx;

					image {
						width: 40rpx;
					}

					text {
						color: $theme-color;
						font-size: 24rpx;
					}
				}
			}
		}

		.center {
			margin: -30rpx 30rpx 0 30rpx;
			border-radius: 20rpx;
			background: #fff;
			padding: 0 30rpx;
			height: 100rpx;
			@include flex-box();
			box-shadow: $global-shade;

			.center-item {
				flex: 1;
				position: relative;
				@include flex-box(row, center, center);

				image {
					height: 38rpx;
					margin-right: 10rpx;
				}

				.tit {
					font-size: 30rpx;
				}
			}

			.act {
				color: $theme-color;

				&::after {
					content: '';
					width: 80rpx;
					height: 4rpx;
					background: $theme-color;
					position: absolute;
					border-radius: 4rpx;
					left: calc(50% - 40rpx);
					bottom: 0;
				}
			}
		}

		.goods-container {
			padding: 20rpx 30rpx 0 30rpx;
			box-sizing: border-box;
		}
	}
</style>